# -*- coding: utf-8 -*-

import psutil
import socket
import threading #多线程库文件
import pythoncom
import wmi
import time

pythoncom.CoInitialize()





def get_ip():
    
    hostname = socket.gethostname()
    print('主机名：',hostname)
    print('\n')
    
    ip = socket.gethostbyname(hostname)
    print('当前IP地址：',ip)
    print('\n')


def getMemCpu():
    
#    rooot = psutil.users()
#    print('当前登录用户：',rooot)
    
#    data = psutil.virtual_memory()
#    zongneic = data.total #总内存
#    print('总内存：',zongneic)
    
    zneic = psutil.virtual_memory() #总内存
    zneic2 = str(round(zneic.total/1024/1024))
    zneic3 = str(round(zneic.total/1024/1024/1024))
#    print("总内存：" + zneic2 + "M")
#    print("总内存：" + zneic3 + "G")
    print("总内存：" + "{:.2f}".format(float(zneic2)) + "MB\t ≈ " + "{:.2f}".format(float(zneic3)) + "GB")
    print('\n')
    
    shengneic = str(zneic.available/1024/1024) #可用内存
    shengneic2 =str(zneic.available/1024/1024/1024)
    
    Ysneic = str(round(zneic.used/1024/1024)) #已使用内存
    Ysneic2 = str(round(zneic.used/1024/1024/1024))
    bfbneic = str(round(zneic.percent)) # 空闲内存百分比
    kYbfbneic = str(100 - round(zneic.percent)) #可使用百分比
    print("已使用：" + "{:.2f}".format(float(Ysneic)) + "MB\t ≈ " + "{:.2f}".format(float(Ysneic2)) + "GB\t 已使用 ≈ " + "{:.2f}".format(float(bfbneic)) + " %")
    print('\n')
    print("空闲内存：" + "{:.2f}".format(float(shengneic)) + " MB\t ≈ " + "{:.2f}".format(float(shengneic2)) + " GB\t 空闲内存 ≈ " + "{:.2f}".format(float(kYbfbneic)) + " %")
    
#    neichunshiyongqink = "内存使用情况 : %d " % (int(round(zneic.percent)))+ "%" #内存占用情况
#    print('内存占用情况：',neichunshiyongqink)
    print('\n')
    
    GsCpu = psutil.cpu_count(logical=False) #查看CPU的核数
    print("CPU核数：",GsCpu)
    print('\n')
    
    CPUchuliqi = psutil.cpu_count()
    print("CPU处理器数量/线程数：",CPUchuliqi)
    print('\n')
    
    cpu = "%0.2f" % psutil.cpu_percent(interval=1) + "%"  # CPU占用情况
    print('CPU占用情况：',cpu)
    print('\n')
#    print('\n')
#    Cpuwanchendu = psutil.cpu_times()
#    print('CPU完成信息：',Cpuwanchendu)
#    print('\n')
#    Cipan = psutil.disk_io_counters()
#    print('磁盘信息：',Cipan)
#    print('\n')
#    CipanWz = psutil.disk_partitions()
#    print('磁盘完整信息：',CipanWz)
#    print('\n')
#    fenqub = psutil.disk_usage('/')
#    print('分区状态信息：',fenqub)
#    print('\n')
#    network = psutil.net_io_counters()
#    print('网络Io信息:',network)
#    print('\n')
#    netjieko = psutil.net_io_counters(pernic=True)
#    print('网络各接口信息：',netjieko)
   
    

def get_disk_info():
    """
    获取物理磁盘信息。
    """
    tmplist = []
    c = wmi.WMI()
    for physical_disk in c.Win32_DiskDrive():
        tmpdict = {}
        tmpdict["硬盘名称："] = physical_disk.Caption
        tmpdict["容量大小："] = '{:.2f}'.format(int(physical_disk.Size) / 1024 / 1024 / 1024) + " G"
        tmplist.append(tmpdict)
    return tmplist



def get_fs_info():
    """
    获取文件系统信息
    包含分区的大小、已用量、可用量、使用率、挂载点信息
    """
    tmplist = []
    c = wmi.WMI()
    for physical_disk in c.Win32_DiskDrive():
        for partition in physical_disk.associators("Win32_DiskDriveToDiskPartition"):
            for logical_disk in partition.associators("Win32_LogicalDiskToPartition"):
                tmpdict = {}
                tmpdict["Caption"] = logical_disk.Caption
                tmpdict["DiskTotal"] = int(logical_disk.Size) / 1024 / 1024 / 1024  #总容量空间
                tmpdict["UseSpace"] = (int(logical_disk.Size) - int(logical_disk.FreeSpace)) / 1024 / 1024 / 1024 #使用空间
                tmpdict["FreeSpace"] = int(logical_disk.FreeSpace) / 1024 / 1024 / 1024 #自由空间
                tmpdict["Percent"] = int(100.0 * (int(logical_disk.Size) - int(logical_disk.FreeSpace)) / int(logical_disk.Size)) #已使用百分比
                tmpdict["available"] = int(100.0 - int(int(100.0 * (int(logical_disk.Size) - int(logical_disk.FreeSpace)) / int(logical_disk.Size))))
                tmplist.append(tmpdict)
    return tmplist


def get_disk():
    disk = get_disk_info()
    print(disk)
    #获取磁盘信息
    fs = get_fs_info()
    for f in fs:
        disk_name = f['Caption'] #磁盘名
        DiskTotal = f['DiskTotal'] #磁盘大小 单位G
        disk_UseSpace = f['UseSpace'] #已用磁盘大小 单位G
        disk_usage = f['Percent'] #已使用空间百分比
        disk_available = f['available'] #剩余百分比
        disk_FreeSpace = f['FreeSpace'] #剩余可用磁盘大小 单位G
        print('\n')
        print('磁盘名：{}  磁盘大小：{}G   已用空间：{}G  剩余可用空间：{}G '.format(disk_name,round(DiskTotal,2),round(disk_UseSpace,2),round(disk_FreeSpace,2)))
        print('已使用:' + '{:.2f}'.format(disk_usage) + " %\t 剩余：" + '{:.2f}'.format(disk_available) + " %\n")


    












threads = []

threads.append(threading.Thread(target=get_ip))

threads.append(threading.Thread(target=getMemCpu))


if __name__ == "__main__":
    
#  getMemCpu()
#  get_ip()
    for t in threads:
        t.start()
        t.join(1) #等待一秒
time.sleep(1)   
get_disk()
        
        
        
        
        
        
        
        